package com.example.easybuy.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.easybuy.pojo.Products;
import com.example.easybuy.pojo.ProductsManage;
import com.example.easybuy.pojo.SolrPage;
import com.example.easybuy.service.ProductsManageService;
import com.example.easybuy.service.ProductsService;
import org.apache.log4j.Logger;
import org.apache.solr.client.solrj.SolrServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
public class ProductsController {
    private Logger logger = Logger.getLogger(ProductsController.class);

    @Autowired
    private ProductsService productsService;

    @Autowired
    private ProductsManageService productsManageService;


    /**
     * 添加数据到Solr
     * @return
     */
    @RequestMapping("addPro")
    @ResponseBody
    public List<Products> addProSolr(){
        logger.debug("ProductsController addPro");
        List<Products> productsList=null;
        try {
            productsList=productsService.addProducts();
            logger.debug("ProductsController addPro result: "+productsList);
        } catch (IOException e) {
            e.printStackTrace();
            logger.debug("ProductsController addPro result: "+productsList+"is error ",e);
        } catch (SolrServerException e) {
            e.printStackTrace();
            logger.debug("ProductsController addPro result: "+productsList+"is error ",e);
        }
        return productsList;
    }


    /**
     * 模糊查询
     * 根据三级分类id查询
     * 查询所有商品
     * 分页查询
     * @param
     * @return
     */
    @RequestMapping("/proList")
    @ResponseBody
    @CrossOrigin
    public SolrPage<Products> proList(String name,String classifythree,String classifyone,String currentPage){
        logger.debug("ProductsController proList name: "+name+" classifythree: "+classifythree+" classifyone: "+classifyone+" currentPage: "+currentPage);
        SolrPage<Products> page=null;
        try{
            page=productsService.proList(name,classifythree,classifyone, currentPage);
            logger.debug("ProductsController proList result: "+page);
        }catch (Exception e){
            e.printStackTrace();
            logger.debug("ProductsController proList name: "+name+" classifythree: "+classifythree+" classifyone: "+classifyone+" currentPage: "+currentPage+" is error ",e);
        }
        return page;
    }

    /**
     * 查询商品的id进行详情页展示
     * 返回list
     * @return
     */
    @RequestMapping("goodsById")
    @ResponseBody
    @CrossOrigin
    public List<Products> goodsById(String id){
        logger.debug("ProductsController goodsById id: "+id);
        List<Products> productsList=productsService.goodsById(id);
        logger.debug("ProductsController goodsById id: "+id+" productsList : "+productsList);
        return productsList;
    }


    /**
     * 根据id查询商品对象
     * 返回对象
     * @return
     */
    @RequestMapping("goodsByIdView")
    @ResponseBody
    @CrossOrigin
    public Products goodsByIdView(String id){
        logger.debug("ProductsController goodsByIdView id: "+id);
        Products products=productsService.goodsByIdView(id);
        logger.debug("ProductsController goodsByIdView id: "+id+" products : "+products);
        return products;
    }


    /**
     * 根据id删除商品
     * @param pid
     * @return
     */
    @RequestMapping("/deletePro")
    @ResponseBody
    @CrossOrigin
    public String deletePro(String pid){
        logger.debug("ProController deletePro pid:"+pid);
        String deleteCount= null;
        try {
            deleteCount = productsService.deletePro(pid);
        } catch (IOException                       e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return deleteCount;
    }


    /**
     * 删除所有数据
     * 用于测试
     * @param
     * @return
     */
    @RequestMapping("/delView")
    @ResponseBody
    public String delView(){
        logger.debug("ProController delView ");
        String deleteQueryCount= null;
        try {
            deleteQueryCount = productsService.delView();
            logger.debug("ProController delView deleteQueryCount： "+deleteQueryCount);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SolrServerException e) {
            e.printStackTrace();
        }
        return deleteQueryCount;
    }

    /*
    添加商品到solr
     */
    @RequestMapping("addProductsManage")
    @ResponseBody
    @CrossOrigin
    public String addProductsManage(){
        logger.debug("ProController addProductsManage...start... ");
        //查询最新添加的对象
        ProductsManage productsManage=new ProductsManage();
        productsManage=productsManageService.getProductsNew();
        logger.debug("ProController addProductsManage-productsManage："+productsManage);
        //把最新添加的对象添加到solr
        Products products=new Products();
        products.setId(String.valueOf(productsManage.getId()));
        products.setClassifyone(productsManage.getClassifyone());
        products.setClassifytwo(productsManage.getClassifytwo());
        products.setClassifythree(productsManage.getClassifythree());
        products.setClassifythree(productsManage.getIsDelete());
        products.setName(productsManage.getName());
        products.setDescribe(productsManage.getDeescribe());
        products.setPrice(productsManage.getPrice());
        products.setStock(productsManage.getStock());
        products.setFileName(productsManage.getFileName());
        products.setIsDelete(productsManage.getIsDelete());
        logger.debug("ProController addProductsManage-getProductsNew-products： "+products);
        String success = productsService.addProductsManage(products);
        logger.debug("ProController addProductsManage-success： "+success);
        return success;
    }

}
